package com.yiwyn.springmixedsql.service;

import com.yiwyn.springmixedsql.domain.TestUsers;
import com.yiwyn.springmixedsql.mapper.TestUsersMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;

@Slf4j
@Service
public class UserMixedService {

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    public void getUser(Long id) throws SQLException {
        long start = System.nanoTime();
        TestUsers testUsers = null;
        SqlSession sqlSession = null;
        try {
            sqlSession = sqlSessionFactory.openSession();
            TestUsersMapper userMapper = sqlSession.getMapper(TestUsersMapper.class);
            testUsers = userMapper.selectByPrimaryKey(id);
        } catch (Exception e) {
            log.error("出现异常", e);
        } finally {
            sqlSession.close();
        }
        // endregion

        long end = System.nanoTime();
        log.info("消耗时间:{}", end - start);
    }


}
